/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function loadXMLDoc() //fungsi untuk load xmlhttp object
{
    var xmlHttp=null;
    try{
        xmlHttp = new XMLHttpRequest();
    }
    catch(e){
        //IE
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function viewChatTextArea(){
    //get object
    var xh = loadXMLDoc();
    var url = "chatForm.php";

    xh.onreadystatechange = view;

    function view(){
        if (xh.readyState==4 && xh.status==200){
        document.getElementById("formChat").innerHTML=xh.responseText;
    		}
    //		else {
    //			alert("Problem retrieving data:" + xh.statusText);
    //		}
    }
    //URLnya
    //onreadystatechange

    xh.open("GET",url,true);
    xh.send(null);
    viewChatBody();
    reloadChatBody();

}

function viewChatArea(){
    //get object
    var xh = loadXMLDoc();
    var url = "chat_area.php";

    xh.onreadystatechange = view;

    function view(){
        if (xh.readyState==4 && xh.status==200){
        document.getElementById("chatArea").innerHTML=xh.responseText;
    		}
    //		else {
    //			alert("Problem retrieving data:" + xh.statusText);
    //		}
    }
    //URLnya
    //onreadystatechange

    xh.open("GET",url,true);
    xh.send(null);
}

var chat_interval;

function viewChatBody(){
    var xh = loadXMLDoc();
    var url = "chat_text.php";

    xh.onreadystatechange = view;
    function view(){
        if (xh.readyState==4 && xh.status==200){
        document.getElementById("bodyChat").innerHTML=xh.responseText;
    		}
    //		else {
    //			alert("Problem retrieving data:" + xh.statusText);
    //		}
    }
    //URLnya
    //onreadystatechange

    xh.open("GET",url,true);
    xh.send(null);
}

function reloadChatBody(){

    clearInterval(chat_interval);
    chat_interval = setInterval('viewChatBody()',1000);
}

function endChat(){
     var xh = loadXMLDoc();
    var url = "chat_text.php?end=1";

    xh.onreadystatechange = view;
    function view(){
        if (xh.readyState==4){
        //document.getElementById("bodyChat").innerHTML=xh.responseText;
			var parent = document.getElementById('formChat');
			var anak = document.getElementById('form_chat');
			parent.removeChild(anak);
    		}
    //		else {
    //			alert("Problem retrieving data:" + xh.statusText);
    //		}
    }
    //URLnya
    //onreadystatechange

    xh.open("GET",url,true);
    xh.send(null);
}